﻿using System;
using RpcClient;
using RpcHelper;
using RpcModel;
using RpcModularModel.Resource;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
    internal class ResourceService : IResourceService
    {
        private readonly IResourceCollect _Resource;

        private readonly IResourceShieldCollect _ShieId;
        private readonly IResourceModularCollect _Modular;
        public ResourceService(IResourceCollect resource, IResourceShieldCollect shield)
        {
            this._ShieId = shield;
            this._Resource = resource;
        }
        public void CancelShieId(Guid resourceId)
        {
            ResourceShield shield = this._ShieId.Get(resourceId);
            this._ShieId.Delete(shield);
        }
        public void SetBeOverTime(Guid resourceId,DateTime? overTime)
        {
            ResourceShield shield = this._ShieId.Get(resourceId);
            _ShieId.SetBeOverdueTime(shield, overTime);
        }
        public DateTime? GetBeOverTime(Guid resourceId)
        {
            ResourceShield shield = this._ShieId.Get(resourceId);
            return shield.BeOverdueTime == 0 ? null : shield.BeOverdueTime.ToDateTime();
        }
        public long ResourceShieId(Guid id, DateTime? overTime)
        {
            ResourceData resource = this._Resource.Get(id);
            if (resource.ResourceState == ResourceState.失效)
            {
                throw new ErrorException("rpc.store.resource.already.invalid");
            }
            ResourceModular modular = this._Modular.Get(resource.ModularId);
            if (modular.ResourceType != ResourceType.API接口)
            {
                throw new ErrorException("rpc.store.resource.not.support");
            }
            return this._ShieId.Add(new ResourceShield
            {
                ApiVerNum = resource.VerNum,
                BeOverdueTime = overTime.HasValue ? overTime.Value.ToLong() : 0,
                ResourceId = resource.Id,
                ResourcePath = resource.ResourcePath,
                RpcMerId = modular.RpcMerId,
                ServerId = 0,
                SortNum = 3,
                SystemType = modular.SystemType,
                ShieKey = string.Join('_', modular.RpcMerId, modular.SystemType, "0", resource.VerNum)
            });
        }
        public ResourceDatum[] Query(ResourceQueryParam query, IBasicPage paging, out long count)
        {
            ResourceData[] list = this._Resource.Query(query, paging, out count);
            if (list.Length == 0)
            {
                return null;
            }
            Guid[] ids = this._ShieId.CheckIsShieId(list.ConvertAll(a => a.Id));
            ResourceDatum[] datas = list.ConvertMap<ResourceData, ResourceDatum>((a, b) =>
            {
                b.IsShieId = ids.IsExists(a.Id);
                return b;
            });
            return datas;
        }
    }
}
